<template>
  <div class="container">
    <div class="login-box">
      <div class="img-box"><img src="@/assets/img/common/logo.jpg"></div>
      <el-form ref="loginFormRef" label-width="0px" class="login-form" :model="formData" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input v-model="formData.username" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formData.password" type="password" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { request } from '@/network/request'
export default {
  name: "Login",
  data() {
    return {
      formData: {
        username: "wangzekai",
        password: "123456"
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async flag => {
        if (!flag) return this.$message.warning("请输入符合格式的内容!")
        if (flag) {
          // 表单验证成功，开始发送网络请求
          const {data:res} = await request({
            method: 'post',
            url: '/login',
            data: this.formData
          })
          if (res.meta.status === 200) {
            // 登录成功
            // console.log(res);
            // 把token信息存储到sessionStorage/LocalStorage
            window.sessionStorage.setItem("token", res.data.token)
            // 跳转页面
            this.$router.replace("/home")
            return this.$message.success("登录成功!")
          } else {
            // 登录失败//用户名不存在//账号或者密码错误
            return this.$message.error("登录失败，请检查账号密码!")
          }
        }
      })
    }
  }
}
</script>

<style lang='less' scoped>
.container {
  width: 100%;
  height: 100%;
  background: url('https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg') no-repeat;
  background-size: 100% 100%;

  .login-box {
    width: 350px;
    height: 350px;
    background-color: #00000060;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .img-box {
      position: absolute;
      left: 50%;
      right: 50%;
      transform: translateX(-50%);
      top: -20%;
      border-radius: 50%;
      box-shadow: 0 0 10px rgba(255, 255, 255, .4);
      width: 130px;
      height: 130px;

      img {
        border-radius: 50%;
        width: 100%;
      }
    }

    .login-form {
      position: absolute;
      top: 30%;
      width: 100%;
      padding: 0 10px;
      box-sizing: border-box;

      .btns {
        display: flex;
        justify-content: center;
      }
    }
  }
}
</style>